<?php
/**
 * 规格模型
 * User: lxq
 * Date: 2018/3/15
 * Time: 14:40
 */
class specModel  extends Model {

    public function __construct()
    {
        parent::__construct('spec');
    }

    /**
     * 获取列表分页
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return mixed
     */
    public function getSpecListPage($condition = array(), $field = '*', $page = 0, $order = 'add_time desc'){
        return $this->field($field)->where($condition)->page($page)->order($order)->select();
    }

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
    public function getSpecInfo($condition = array(), $field = '*'){
        return $this->field($field)->where($condition)->find();
    }

    /**
     * 插入一条记录
     * @param array $params
     * @return mixed
     */
    public function addSpec($params)
    {
        return $this->insert($params);
    }


    /**
     * 更新记录
     * @param array $condition
     * @param array $params
     * @return mixed
     */
    public function updateSpec($condition, $params)
    {
        return $this->where($condition)->update($params);
    }

    /**
     * 获取记录数组
     * @param array $condition
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public function getSpecList($condition = array(), $field = '*', $order = 'spec_id desc'){
        return $this->field($field)->where($condition)->order($order)->select();
    }

    /**
     * 删除记录
     * @param array $condition
     */
    public function deleteSpec($condition){
       return $this->where($condition)->delete();
    }

    /**
     * 增加某个规格销量
     * @param int $spec_id
     * @param int $sum
     */
     public function addSpecSales($spec_id,$sum)
     {
        return $this->where(array('spec_id'=>$spec_id))->setInc('sales',$sum);
     }

    /**
     * 减少某个规格销量
     * @param int $spec_id
     * @param int $sum
     */
    public function  reduceSpecSales($spec_id,$sum)
    {
        return $this->where(array('spec_id'=>$spec_id))->setDec('sales',$sum);
    }
    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
    public function getSpecInfo_v1($condition = array()){
        return $this->field("mix(price) as price , SUM(stock) as stock ,max(market_price) as market_price ")->where($condition)->find();
    }

    /**
     * 获取一条记录的详细信息
     * @param array $condition
     * @param string $field
     * @return mixed
     */
    public function getSpecInfo_v2($condition = array()){
        return $this->field("min(price) as price , SUM(stock) as stock ,min(market_price) as market_price ")->where($condition)->find();
    }

}